package ThreadOperation;

//停止线程
//1.正常停止
//2.使用标志位
//3.不要使用stop或者destroy
public class ThreadStop implements Runnable
{
    private boolean flag = true;

    @Override
    public void run()
    {
        int i = 0;
        if (flag == false)
        {
            System.out.println("Thread stopped at " + i);
        }
        while (flag)
        {
            System.out.println("Thread" + i++);
        }
    }

    public void stop()
    {
        this.flag = false;
    }

    public static void main(String[] args)
    {
        ThreadStop st = new ThreadStop();
        new Thread(st).start();
        for (int i = 0; i < 101; i++)
        {
            System.out.println("main" + i);
            if (i == 100)
            {
                st.stop();
                System.out.println("Thread Stopped");
            }
        }
    }
}



